﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using Manager2070.UI.Properties;
using System.Management;

namespace Manager2070.UI
{
    public partial class frmMain : Form
    {
        Library.Game _game = new Library.Game();
        Library.Patcher _patcher = new Library.Patcher();

        public frmMain()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.Hide();
            this.WindowState = FormWindowState.Minimized;

            if (! _game.IsGameInstalled)
            {
                ni.ShowBalloonTip(1000, "Welcome", Resources.ResourceManager.GetString("GAME_REGISTRY_KEY_NOT_FOUND"), ToolTipIcon.Error);
                DisableMenu();
            }
            else
            {
                ni.ShowBalloonTip(500, "Welcome", string.Format(Resources.ResourceManager.GetString("GAME_FOUND_AT_LOCATION"), System.Configuration.ConfigurationManager.AppSettings["game"] , _game.InstallationDirectory), ToolTipIcon.Info);
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            QuitApp();
        }

        private void DisableMenu()
        {
            ni.ContextMenuStrip = null;
            ni.Click += new EventHandler(ni_Click);
        }

        private void QuitApp()
        {
            try { ni.Click -= new EventHandler(ni_Click); }
            catch { }

            ni.Visible = false;
            ni.Dispose();

            Application.Exit();
        }

        void ni_Click(object sender, EventArgs e)
        {
            QuitApp();
        }

        private void launchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StartGame(true);
        }

        private void StartGame(bool isOnline)
        {
            if (_patcher != null)
                _patcher.Quit();

            _patcher = new Library.Patcher();
            _game.OnGameReady += new EventHandler(_game_OnGameReady);
            _game.OnGameNotReady += new EventHandler(_game_OnGameNotReady);
            _game.OnGameEnded += new EventHandler(_game_OnGameEnded);
            _game.LaunchGame(true);
        }

        void _game_OnGameNotReady(object sender, EventArgs e)
        {
            Invoke(new MethodInvoker(delegate() 
                { 
                    _patcher.GameNotReady(Resources.ResourceManager.GetString("PATCHER_STATUS_UBILAUNCHER_ENDED"));
                    ni.ShowBalloonTip(1000, "Info", Resources.ResourceManager.GetString("PATCHER_STATUS_UBILAUNCHER_ENDED"), ToolTipIcon.Info);
                }));
        }

        void _game_OnGameEnded(object sender, EventArgs e)
        {
            MessageBox.Show("Anno Game Ended.  Restore original patch rda ?");
        }

        void _game_OnGameReady(object sender, EventArgs e)
        {
            Invoke(new MethodInvoker(delegate() { _patcher.ShowPatchApplyScreen(); }));
        }
    }
}
